/**
 * 537. 复数乘法
 * https://leetcode-cn.com/problems/complex-number-multiplication/
 */
public class Solutions_537 {
    public static void main(String[] args) {
//        String num1 = "1+1i", num2 = "1+1i";  // output: "0+2i"
        String num1 = "1+-1i", num2 = "1+-1i";  // output: "0+-2i"

        String result = complexNumberMultiply(num1, num2);
        System.out.println(result);
    }

    /**
     * 复数相乘公式：(ac - bd) + (ad + bc)
     */
    public static String complexNumberMultiply(String num1, String num2) {
        String[] num1Arr = num1.split("\\+");
        String[] num2Arr = num2.split("\\+");

        int a = Integer.valueOf(num1Arr[0]);
        int b = Integer.valueOf(num1Arr[1].substring(0, num1Arr[1].length() - 1));
        int c = Integer.valueOf(num2Arr[0]);
        int d = Integer.valueOf(num2Arr[1].substring(0, num2Arr[1].length() - 1));

        int left = a * c - b * d;
        int right = a * d + b * c;
        return left + "+" + right + "i";
    }
}
